﻿using System;
using System.ServiceProcess;
using NLog;
using TfsGiggle.Core;

namespace TfsGiggle.EngineService
{
	static class Program
	{
        private static LoggerWithEventId _logger = (LoggerWithEventId)LogManager.GetCurrentClassLogger(typeof(LoggerWithEventId));

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		static void Main()
		{
			ServiceBase[] ServicesToRun;
			ServicesToRun = new ServiceBase[] 
			{ 
				new TfsGiggleService() 
			};
			AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
			ServiceBase.Run(ServicesToRun);
		}

		private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
		{
			Exception ex = e.ExceptionObject as Exception; 
			if (ex == null) ex = new Exception(e.ExceptionObject.ToString());
			_logger.FatalExceptionWithEventId(EventId.CurrentDomainUnhandledException, "Unhandled exception caught.", ex);
		}
	}
}
